define(
    ["PDUtilDir/util", "PDUtilDir/dataService", "AppPathDir/order/orderDoneInfos/replaceVisitor", "PDUtilDir/grid", "PDAppDir/properties", "PDUtilDir/slidebar",
        "css!AppPathDir/order/orderDoneInfos/views/css/css"],
    function (Util, DataService, ReplaceVisitor, Grid, Properties, Slidebar) {
        function detailSupportInit($scope, $compile) {
            return new detailSupport($scope, $compile);
        }

        function detailSupport($scope, $compile) {
            this.$scope = $scope;
            this.$compile = $compile;

            this.entity = {
                pays: new Array()
            };
            this.cache = {
                fileServer: Properties.URL_FILE_SERVER_URL,
                images: new Array()
            }
        }

        detailSupport.fn = detailSupport.prototype;
        detailSupport.fn.digest = function () {
            this.$scope.$digest();
        };
        detailSupport.fn.selectVisitorFun = function(){
            return this.selectVisitorSupport = new ReplaceVisitor(this.$scope, this.$compile);
        };
        detailSupport.fn.replaceFun = function () {
            var _this = this,
                replaceData = DataService.getObject('order.orderInfo.visitor.replace');

            $.ajax({
                url : getServer() + "/flea/hclv/visitor/order/updateVisitorByOrderId.htmls",
                method : 'post',
                data : {
                    'model' : JSON.stringify({
                        orderNo: _this.entity.order.orderNo,
                        oldVisitorId: replaceData.oldId,
                        newVisitorId: replaceData.visitor.id
                    })
                },
                success : function(data) {
                    if(data.status != 200){
                        return ;
                    }
                    selectedArray = _this.entity.visitors,
                        len = selectedArray.length;

                    for(i = 0; i < len; i++){
                        if (selectedArray[i].id == replaceData.oldId){
                            selectedArray.splice(i, 1);
                            break;
                        }
                    }
                    selectedArray.push(replaceData.visitor);

                    _this.grid.refresh();

                    Util.alert(data.message);
                }
            });
        };
        // 删除
        detailSupport.fn.delSupport = {
            run : function(visitorId) {
                var _this = angular.element('#OrderDetailForVisitorGirdDivId').scope().detailSupport;

                Util.confirm("是否要删除", function() {

                    $.ajax({
                        url : getServer() + "/flea/hclv/visitor/order/delVisitorByOrderNoAndVisitorId.htmls",
                        method : 'post',
                        data : {
                            'model' : JSON.stringify({
                                orderNo: _this.entity.order.orderNo,
                                visitorId: visitorId
                            })
                        },
                        success : function(data) {
                            if(data.status != 200){
                                return ;
                            }
                            _this.grid.refresh();
                            Util.alert(data.message);
                        }
                    });
                });
            }
        };

        detailSupport.fn.run = function (id) {
            var _this = this;
            this.entity = {
                id: id,
                visitors: new Array()
            };
//				_this.entity.statusName = _this.$scope.cache.statusMap.get(_this.entity.status);

            this.slidebars = Slidebar({
                size: 'lg',
                url: getServer()
                + "/static/app/order/orderDoneInfos/views/detail.html",
//					close : true,
                $scope: this.$scope,
                $compile: this.$compile,
                afterLoad: function ($Panel) {
                    _this.initialization();
                }
            });
        };
        detailSupport.fn.initialization = function () {
            var _this = this;
            $.ajax({
                url: getServer() + "/flea/hclv/visitor/order/findDetailById.htmls",
                data: {
                    model: _this.entity.id
                },
                success: function (data) {
                    _this.entity = $.extend(_this.entity, data.data);
                    _this.entity.order.statusName = _this.$scope.cache.statusMap.get(_this.entity.order.status);
                    _this.renderGrid();
                    _this.digest();
                }
            });
            if (_this.$scope.cache.certificates.length == 0) {
                $.ajax({
                    url: getServer() + "/flea/hclv/getCertificates.htmls",
                    success: function (data) {
                        _this.$scope.cache.certificates = data.data;
                        var certificates = data.data,
                            len = certificates.length,
                            obj = new Map();

                        for (var i = 0; i < len; i++) {
                            obj.set(certificates[i].dictCode, certificates[i].dictName);
                        }
                        _this.$scope.cache.certificatesObject = obj;
                        _this.renderGrid();
                    }
                });
            }
        };
        detailSupport.fn.renderGrid = function () {
            var _this = this;
            if (_this.$scope.cache.certificates.length > 0 && _this.entity.visitors.length > 0) {
                _this.grid = Grid({
                    id: "OrderDetailForVisitorGirdDivId",
                    index: true,
                    multi: true,
                    pagination: false,
                    cache: false,
                    layout: [{
                        name: "中文名",
                        field: "zhName",
                        width: "60px",
                        sort: true
                    }, {
                        name: "英文名",
                        field: "enName",
                        width: "60px",
                        sort: true
                    }, {
                        name: "性别",
                        field: "sex",
                        width: "40px",
                        format: function (e) {
                            if (e.row.sex == "1" || e.row.sex == 1)
                                return '男';
                            else
                                return '女';
                        }
                    }, {
                        name: "年龄",
                        width: "40px",
                        field: "age"
                    }, {
                        name: "联系电话",
                        width: "80px",
                        field: "phone"
                    }, {
                        name: "证件类型",
                        field: "certificates",
                        width: "60px",
                        format: function (e) {
                            return _this.$scope.cache.certificatesObject.get(e.row.certificates);
                        }
                    }, {
                        name: "证件号码",
                        field: "certificatesNumber"
                    }, {
                        name: "操作",
                        field: "createTime",
                        sort: false,
                        width: '60px',
                        format: function (e) {
                            return '<a href="javascript:angular.element(\'#OrderDetailForVisitorGirdDivId\').scope().detailSupport.selectVisitorFun().run(\'' + e.row._index + '\',\''+ e.row.id +'\')">更換</a>' +
                                '<a href="javascript:angular.element(\'#OrderDetailForVisitorGirdDivId\').scope().detailSupport.delSupport.run(\''+ e.row.id +'\')">删除</a>';
                        }
                    }],
                    toolbar: [],
                    data: _this.entity.visitors
                });
            }
        }
        return detailSupportInit;
    });